package com.aishua.aishuawallet.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.aishua.aishuawallet.R;
import com.aishua.aishuawallet.utils.LogUtils;
import com.google.zxing.Result;
import com.google.zxing.client.result.ParsedResult;
import com.mylhyl.zxing.scanner.OnScannerCompletionListener;
import com.mylhyl.zxing.scanner.ScannerView;
import com.mylhyl.zxing.scanner.common.Scanner;

/**
 * Created by aishua on 2018/6/20.
 */

public class ScannerActivity extends Activity implements OnScannerCompletionListener {

    private ScannerView scanner_view;
    private ProgressDialog progressDialog;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        initView();
    }

    private void initView() {

        scanner_view = findViewById(R.id.scanner_view);
        scanner_view.setOnScannerCompletionListener(this);
        scanner_view.setMediaResId(R.raw.beep);//设置扫描成功的声音
        scanner_view.setDrawText("将二维码放入框内", true);
        scanner_view.setDrawTextColor(Color.RED);
        scanner_view.setScanMode(Scanner.ScanMode.QR_CODE_MODE);
        scanner_view.setLaserGridLineResId(R.mipmap.zfb_grid_scan_line);//网格图
        scanner_view.setLaserFrameBoundColor(0xFF26CEFF);//支付宝颜色
        //显示扫描成功后的缩略图
        scanner_view.isShowResThumbnail(true);
        //全屏识别
        scanner_view.isScanFullScreen(false);
        //隐藏扫描框
        scanner_view.isHideLaserFrame(false);

    }

    @Override
    public void onScannerCompletion(final Result rawResult, ParsedResult parsedResult, Bitmap barcode) {

        LogUtils.v("ScannerActivity" + rawResult.getText());
        if (rawResult == null) {
            Toast.makeText(this, "未发现二维码", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        Toast.makeText(this, rawResult.getText(), Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.putExtra("result",rawResult.getText());
        setResult(1001,intent);
        finish();
    }

    @Override
    protected void onResume() {
        scanner_view.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        scanner_view.onPause();
        super.onPause();
    }
    void showProgressDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("请稍候...");
        progressDialog.show();
    }

    void dismissProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }
}
